package com.snail.snailvr.utils;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.Snackbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.snail.snailvr.R;
import com.snail.snailvr.VRApp;
import com.snail.snailvr.views.BaseActivity;

/**
 * 自定义Toast
 * Created by snail on 16/4/18.
 */
public class ToastUtils {
    public static final int LENGTH_SHORT = 2000;

    /**
     * common toast
     * @param msgId
     */
    public static void showToast(int msgId) {
        showToast(VRApp.getInstance().getString(msgId));
    }

    public static void showToast(String msg) {
        Activity activity= BaseActivity.getCurrentActivity();
        if(activity!=null)
            makeToastColorShow(activity,activity.findViewById(android.R.id.content),msg);
        else {
            Toast.makeText(VRApp.getInstance(), msg, Toast.LENGTH_SHORT).show();
        }
    }

    public static void makeToastColorShow(Context context, View view, String str) {
        if (view != null) {
            Snackbar bar = Snackbar.make(view, str, Snackbar.LENGTH_SHORT);
            bar.getView().setBackgroundColor(context.getResources().getColor(R.color.toast_background_color));
            bar.show();
        }
    }


    /**
     * 播放器界面toast提示
     * @param msg
     */
    public static void showPlayerToast(String msg) {
        Context context=VRApp.getInstance();
        Toast toast = new Toast(context);
        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0,
                context.getResources().getDimensionPixelSize(R.dimen.toast_margin_top));
        show(context,msg,toast);
    }

    /**
     * 播放器界面toast提示
     * @param msg
     */
    public static void showPlayerErrorToast(String msg) {
        Context context=VRApp.getInstance();
        Toast toast = new Toast(context);
        show(context,msg,toast);

    }

    /**
     * @param msg Toast显示文字
     */
    private static void show(Context context, String msg, Toast toast) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View layout = inflater.inflate(R.layout.common_toast_layout, null);
        TextView contentTv = (TextView) layout.findViewById(R.id.text_content);
        contentTv.setText(msg);

        toast.setView(layout);
        toast.setDuration(LENGTH_SHORT);
        toast.show();
    }

}
